from bald_spider.event import response_received, request_scheduled, spider_opened, spider_closed, item_successful, \
    ignore_discard
from bald_spider.utils.date import now, date_delta


class LogStats:

    def __init__(self,stats):
        self._stats = stats

    @classmethod
    def create_instance(cls,crawler):
        o = cls(crawler.stats)
        crawler.subscriber.subscribe(o.spider_opened,event=spider_opened)
        crawler.subscriber.subscribe(o.spider_closed, event=spider_closed)
        crawler.subscriber.subscribe(o.item_successful, event=item_successful)
        crawler.subscriber.subscribe(o.ignore_discard, event=ignore_discard)
        crawler.subscriber.subscribe(o.response_received,event=response_received)
        crawler.subscriber.subscribe(o.request_scheduled,event=request_scheduled)
        return o

    async def spider_opened(self):
        self._stats['start_time'] = now()

    async def spider_closed(self):
        self._stats['end_time'] = now()
        self._stats['cost_time(s)'] = date_delta(self._stats['start_time'], self._stats['end_time'])

    async def response_received(self,_response,_spider):
        self._stats.inc_value('response_received_count')

    async def request_scheduled(self,_request,_spider):
        self._stats.inc_value('request_scheduled_count')

    async def item_successful(self,_item,_spider):
        self._stats.inc_value('item_successful_count')

    async def ignore_discard(self,_item,exc,_spider):
        self._stats.inc_value('ignore_discard_count')
        reason = exc.msg
        if reason:
            self._stats.inc_value(f'ignore_discard/{reason}')